package com.ibiaoke.order.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.order.entity.GoodsRush;
import com.ibiaoke.order.entity.GoodsRushExample;
import com.ibiaoke.order.mapper.GoodsRushMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class GoodsRushDao extends BaseDao<GoodsRush, GoodsRushExample, Integer, GoodsRushMapper> {

    @Autowired
    private GoodsRushMapper goodsRushMapper;

    @Override
    public GoodsRushMapper getMapper() {
        return goodsRushMapper;
    }

    /**
     * 查询所有未过期抢购
     * @return
     */
    public List<GoodsRush> listRush() {
        GoodsRushExample example = new GoodsRushExample();
        example.createCriteria().andBeginTimeLessThan(new Date()).andEndTimeGreaterThan(new Date()).andDelStatusEqualTo(CommonConsts.NO);
        example.setOrderByClause("begin_time");
        return selectByExample(example);
    }

    /**
     * 查询进行中的抢购活动
     * @param goodsId
     * @return
     */
    public List<GoodsRush> listValidRush(Integer goodsId) {
        GoodsRushExample example = new GoodsRushExample();
        GoodsRushExample.Criteria criteria = example.createCriteria().andBeginTimeLessThan(new Date())
                .andEndTimeGreaterThan(new Date()).andDelStatusEqualTo(CommonConsts.NO);
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        example.setOrderByClause("begin_time");
        return goodsRushMapper.selectByExample(example);
    }

    /**
     * 查询所有的抢购活动
     * @return
     * @param goodsId
     */
    public List<GoodsRush> listAllRush(Integer goodsId) {
        GoodsRushExample example = new GoodsRushExample();
        GoodsRushExample.Criteria criteria = example.createCriteria().andDelStatusEqualTo(CommonConsts.NO);
        if(goodsId!=null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        example.setOrderByClause("begin_time");
        return selectByExample(example);
    }

    /**
     * 调用悲观锁来查询促销活动
     * @param rushId
     * @return
     */
    public GoodsRush getGoodsRushByRushIdForUpdate(Integer rushId) {
        return goodsRushMapper.getGoodsRushByRushIdForUpdate(rushId);
    }
}
